﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ICSharpCode.AvalonEdit.Indentation;
using ICSharpCode.AvalonEdit.Utils;
using ICSharpCode.AvalonEdit.Document;


namespace CryForLua_wpf_AvEd
{
    public class Indentation : IIndentationStrategy
    {
        public virtual void IndentLine(TextDocument document, DocumentLine line)
        {
          
            if (document == null)
                throw new ArgumentNullException("document");
            if (line == null)
                throw new ArgumentNullException("line");
            DocumentLine previousLine = line.PreviousLine;

            if (previousLine != null)
            {
                string lineText = document.GetText(previousLine.Offset, previousLine.EndOffset - previousLine.Offset);

               /* if ((lineText.Contains('{') && !lineText.Contains('}')))
                {
                    ISegment previousLineWhitespaces = TextUtilities.GetWhitespaceAfter(document, previousLine.Offset);
                    string segmentText = document.GetText(previousLineWhitespaces);
                    string indentationText = segmentText + "\t\n" + segmentText + "}";
                    ISegment currentLineWhitespaces = TextUtilities.GetWhitespaceAfter(document, line.Offset);
                    document.Replace(currentLineWhitespaces, indentationText);
                }
                else if (lineText.Contains('}'))
                {
                    ISegment whitespace = TextUtilities.GetWhitespaceAfter(document, previousLine.Offset);
                    string text = document.GetText(whitespace);
                    int lastTab = text.LastIndexOf("\t");
                    if (lastTab != -1)
                    {
                        text = text.Remove(lastTab, 1);
                        document.Replace(whitespace, text);
                    }
                }
               /* else if (lineText.Contains("function"))
                {
                    ISegment previousLineWhitespaces = TextUtilities.GetWhitespaceAfter(document, previousLine.Offset);
                    string segmentText = document.GetText(previousLineWhitespaces);
                    string indentationText = segmentText + "\t\n" + segmentText + "end";
                    ISegment currentLineWhitespaces = TextUtilities.GetWhitespaceAfter(document, line.Offset);
                    document.Replace(currentLineWhitespaces, indentationText);
                }
                else*/
                {
                    ISegment indentationSegment = TextUtilities.GetWhitespaceAfter(document, previousLine.Offset);
                    string indentation = document.GetText(indentationSegment);
                    // copy indentation to line
                    indentationSegment = TextUtilities.GetWhitespaceAfter(document, line.Offset);
                    document.Replace(indentationSegment, indentation);
                }
            }
                            
        }

        /// <summary>
        /// Does nothing: indenting multiple lines is useless without a smart indentation strategy.
        /// </summary>
        public virtual void IndentLines(TextDocument document, int beginLine, int endLine)
        {
        }
    }
}
